package com.itcast.googleplay.ui;

import java.util.ArrayList;
import java.util.List;

import com.itcast.googleplay.utils.UIUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;


public class Flowlayout3 extends ViewGroup {
	public static final int horizontalSpacing = UIUtils.dip2px(10);
	public static final int verticalSpacing = UIUtils.dip2px(10);
	private Line line;
	private List<Line> lineList = new ArrayList<Flowlayout3.Line>();

	public Flowlayout3(Context context) {
		super(context);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int left = getPaddingLeft();
		int top = getPaddingTop();
		for (int i = 0; i < lineList.size(); i++) {
			Line line = lineList.get(i);
			line.layout(left, top);
			top += line.lineHeight+verticalSpacing; 
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		line = new Line();
		lineList.clear();
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int usedWidth = width -getPaddingLeft()-getPaddingRight();
		for (int i = 0; i < getChildCount(); i++) {
			View view = getChildAt(i);
			view.measure(0, 0);
			
			int childWidth = view.getMeasuredWidth();
			
			if(line.getLineViewCount() == 0){
				line.addLineView(view);
			}else if(childWidth+line.lineWidth+horizontalSpacing>usedWidth){
				lineList.add(line);

				line = new Line();
				line.addLineView(view);
			}else{
				line.addLineView(view);
			}
			//加上最后一行
			if(i == getChildCount()-1){
				lineList.add(line);
			}
		}
		
		int height = getPaddingBottom()+getPaddingTop();
		for (int i = 0; i < lineList.size(); i++) {
			height += lineList.get(i).lineHeight; 
		}
		height += (lineList.size()-1)*verticalSpacing;
		
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	public class Line{
		public int lineWidth;
		public int lineHeight;
		public List<View> lineViewList = new ArrayList<View>();
		public int getLineViewCount(){
			return lineViewList.size();
		}
		
		public void addLineView(View view){
			lineViewList.add(view);
			view.measure(0, 0);
			int viewWidth = view.getMeasuredWidth();
			int viewHeight = view.getMeasuredHeight();
			
			lineWidth += viewWidth+horizontalSpacing;
			lineHeight = Math.max(lineHeight, viewHeight);
		}
		
		public void layout(int left,int top){
			int totalSurplusWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - lineWidth;
			int surplusWidth = totalSurplusWidth/getLineViewCount();
			
			for (int i = 0; i < getLineViewCount(); i++) {
				View view = lineViewList.get(i);
				view.measure(0, 0);
				
				int width = view.getMeasuredWidth()+surplusWidth;
				int height = view.getMeasuredHeight();
				
				int width32 = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
				int height32 = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
				
				view.measure(width32, height32);
				
				view.layout(left, top, left+width, top+height);
				left += width+horizontalSpacing; 
			}
		}
	}
}
